<?php

class Logs extends Doctrine_EventListener {
	
	protected $manager;
	
	public function Logs(Doctrine_Manager $aplication_manager){
		$this->manager = Doctrine_Manager::getInstance();
	}
		
	public function postTransactionBegin(Doctrine_Event $event){
		//TODO verificação de login, não implementado utilizadores.
		/*if(session_id() && strcasecmp($_SESSION['tipo_sessao'], Utilizadores::utilizador_sistema) == 0){
			
		}*/
		if (isset($_SESSION['current_transaction'])){
			unset($_SESSION['current_transaction']);
		}
		$this->manager->setCurrentConnection('logs_connection');
		$_SESSION['current_transaction'] = new Transaccoes();
		# $_SESSION['current_transaction']->utilizador_id = $_SESSION['id'];
		// Utilizador = 1 pois não há utulizadores ainda implementados.
		$_SESSION['current_transaction']->utilizador_id = 1;
		$_SESSION['current_transaction']->data_inicio = date("Y-m-d H:i:s");
		$this->manager->setCurrentConnection('base_connection');
	}
	
	public function postTransactionCommit(Doctrine_Event $event){
		/*if(session_id() && strcasecmp($_SESSION['tipo_sessao'], Utilizadores::utilizador_sistema) == 0 && isset($_SESSION['current_transaction'])){
			if ($_SESSION['current_transaction']->Operacoes->count() > 0){
				$this->manager->setCurrentConnection('logs_connection');
				$_SESSION['current_transaction']->data_fim = date("Y-m-d H:i:s");
				$_SESSION['current_transaction']->save();
				$this->manager->setCurrentConnection('base_connection');
			}
			unset($_SESSION['current_transaction']);
		}*/
		if (isset($_SESSION['current_transaction'])){
			if ($_SESSION['current_transaction']->Operacoes->count() > 0){
				$this->manager->setCurrentConnection('logs_connection');
				$_SESSION['current_transaction']->data_fim = date("Y-m-d H:i:s");
				$_SESSION['current_transaction']->save();
				$this->manager->setCurrentConnection('base_connection');
			}
			unset($_SESSION['current_transaction']);
		}
	}

}
?>